import readText from '../../lib/FSTool/readText';

const {createLogger, format, transports} = require('winston');
import path from 'path';
import configTool from '../configTool';
import writeText from '../../lib/FSTool/writeText';

const {combine, timestamp, ms, printf} = format;


const sisFormat = printf(({level, message, timestamp}: any) => {
    return `${new Date(timestamp).toLocaleString()} ${level} : ${message}`;
});


let logger: any = null;

const LogTool = {
    getLogger: async () => {
        await configTool.init();

        if (logger) {
            return logger;
        } else {
            logger = createLogger({
                level: 'info',
                format: combine(
                    ms(),
                    timestamp(),
                    sisFormat
                ),
                transports: [
                    new transports.File({
                        filename: path.join(configTool.appBasePath, 'temp/logs/httpServer/info.log'),
                        level: 'info',
                        format: format.json()
                    }),
                    new transports.File({
                        filename: path.join(configTool.appBasePath, 'temp/logs/httpServer/warn.log'),
                        level: 'warn',
                        format: format.json()
                    }),
                    new transports.File({
                        filename: path.join(configTool.appBasePath, 'temp/logs/httpServer/error.log'),
                        level: 'error',
                        format: format.json()
                    }),
                    new transports.Console({
                        level: 'warn',
                        format: format.combine(
                            format.colorize(),
                            format.splat(),
                            sisFormat
                        )
                    })
                ]
            });
        }

        return logger;
    },
    queryLog: async (level: string) => {
        await configTool.init();
        let logPath = '';
        if (level === 'info') {
            logPath = path.join(configTool.appBasePath, 'temp/logs/httpServer/info.log');
        } else if (level === 'warn') {
            logPath = path.join(configTool.appBasePath, 'temp/logs/httpServer/warn.log');
        } else if (level === 'error') {
            logPath = path.join(configTool.appBasePath, 'temp/logs/httpServer/error.log');
        }
        const logTxt = readText(logPath)?.split('\r\n').slice(0, -2);
        return JSON.parse(`[${logTxt}]`);
    },
    clearLog: async (level: string = 'info') => {
        await configTool.init();
        let logPath = '';
        if (level === 'info') {
            logPath = path.join(configTool.appBasePath, 'temp/logs/httpServer/info.log');
        } else if (level === 'warn') {
            logPath = path.join(configTool.appBasePath, 'temp/logs/httpServer/warn.log');
        } else if (level === 'error') {
            logPath = path.join(configTool.appBasePath, 'temp/logs/httpServer/error.log');
        }
        writeText(logPath, '');
    }
};

export default LogTool;